package com.tech.view.component.button;

import com.tech.view.common.SwingTestUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 圆角按钮
 */
public class RoundButton extends JButton implements MouseListener {
	private Color bGColor;
	private Color brighterBGColor;
	private Color brightestBGColor;
	private Color currentBGColor;
	private int radius;

	public RoundButton(String text, Icon icon, Color bGColor, int radius) {
		super(text, icon);
		this.currentBGColor = bGColor;
		brighterBGColor = new Color(Math.min(currentBGColor.getRed() + 8, 255), Math.min(currentBGColor.getGreen() + 8, 255), Math.min(currentBGColor.getBlue() + 8, 255));
		brightestBGColor = new Color(Math.min(currentBGColor.getRed() + 16, 255), Math.min(currentBGColor.getGreen() + 16, 255), Math.min(currentBGColor.getBlue() + 16, 255));
		this.bGColor = bGColor;
		this.radius = radius;
		super.setContentAreaFilled(false);

		addMouseListener(this);
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		RoundButton button1 = new RoundButton("测试按钮",
				// new ImageIcon("D:\\MyDefault\\desktop\\SchoolBooksTrade\\SBT\\src\\main\\resources\\images\\choiceness.png"),
				null,
				Color.GRAY, 10);
		JButton button2 = new JButton("测试按钮");
		button2.setBackground(Color.GRAY);
		SwingTestUtils.test(button1, button2);
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(currentBGColor);
		g2.fillRoundRect(0, 0, super.getSize().width - 1, super.getSize().height - 1, radius, radius);
		super.paintComponent(g2);
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {
		currentBGColor = brightestBGColor;
		repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		currentBGColor = brighterBGColor;
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		currentBGColor = brighterBGColor;
		repaint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		currentBGColor = bGColor;
		repaint();
	}
}